package ru.rchervot.sanasar.client.view;

import ru.rchervot.sanasar.client.model.SearchResultsItem;
import ru.rchervot.sanasar.client.model.SearchResultsItemCandidates;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.inject.Inject;

public class RecordSummaryCandidatesView extends AbstractRecordSummaryView 
implements RecordSummaryCandidatesPresenter.Display {
		
	VerticalPanel personal = new VerticalPanel();
	VerticalPanel history = new VerticalPanel();
	Hyperlink h;
	
	final Label rowNumber = new Label();
	
	private final Label candName = new Label();
	final Label regionName = new Label();
	final Label homeCity = new Label();
	final Label candID = new Label();
	
	final Label contactsPhones = new Label();
	final HTML contactsEmail = new HTML();
	
	final Label languages = new Label();
	final Label categories = new Label();
	
	final HTML empl = new HTML();
	final HTML order = new HTML();
	
	final Label contactDate = new Label();
	final Label contactType = new Label();
	final Label contactResult = new Label();
	final Label contactCreator = new Label();
	final Label contactCreatorName = new Label();
	
	final Label contactNote = new Label();
	
	final HTML updated = new HTML();

	final Hyperlink orderApplicants = new Hyperlink();
	final Label compensation = new Label();
	final Label age = new Label();
	
	final HorizontalPanel pName = new HorizontalPanel();
	final HorizontalPanel pContacts = new HorizontalPanel();
	final HorizontalPanel h2 = new HorizontalPanel();
	final HorizontalPanel h3 = new HorizontalPanel();
	final HorizontalPanel pAction = new HorizontalPanel();
	final HorizontalPanel h9 = new HorizontalPanel();
	
	protected final VerticalPanel basePanel = new VerticalPanel();
	
	@Inject
	public RecordSummaryCandidatesView() {
		super();
		
		basePanel.setWidth("100%");
		basePanel.setStylePrimaryName("RecordSummaryCandidatesView");

		
		basePanel.add(personal);
		basePanel.add(history);
		
		this.initWidget(basePanel);
		
		makeWidget();
	}
	
	void makeWidget() {
		
//	candName.addClickListener(candNameListner);
	
	
		candName.setStylePrimaryName("RecordSummaryCandidatesView_name");
		age.setStylePrimaryName("RecordSummaryCandidatesView_age");
		compensation.setStylePrimaryName("RecordSummaryCandidatesView_comp");
		candID.setStylePrimaryName("RecordSummaryCandidatesView_id");
		regionName.setStylePrimaryName("RecordSummaryCandidatesView_region");
		
		contactsPhones.setStylePrimaryName("RecordSummaryCandidatesView_phones");
		contactsEmail.setStylePrimaryName("RecordSummaryCandidatesView_email");
		
		categories.setStylePrimaryName("RecordSummaryCandidatesView_categories");
		languages.setStylePrimaryName("RecordSummaryCandidatesView_languages");
		
		pAction.setStylePrimaryName("RecordSummaryCandidatesView_pAction");
		
		contactNote.setStylePrimaryName("RecordSummaryCandidatesView_contactNote");
		
		contactDate.setStylePrimaryName("RecordSummaryCandidatesView_contactDate");
		contactType.setStylePrimaryName("RecordSummaryCandidatesView_contactType");
		contactResult.setStylePrimaryName("RecordSummaryCandidatesView_contactResult");
		contactCreator.setStylePrimaryName("RecordSummaryCandidatesView_contactCreator");
		
		
		pName.add(candName);
		pName.add(candID);
		pName.add(regionName);
		pName.add(compensation);
		pName.add(age);


		pContacts.add(contactsPhones);
		pContacts.add(contactsEmail);
		
		pAction.add(contactDate);
		pAction.add(contactType);
		pAction.add(contactResult);
		pAction.add(contactCreatorName);


		personal.add(pName);
		personal.add(pContacts);
		
		personal.add(empl);
		
		personal.add(categories);
		personal.add(languages);

		history.add(order);
		history.add(contactNote);
		history.add(pAction);
		history.add(h9);
		history.add(updated);
	
}

	@Override
	public void updateDisplay(SearchResultsItem itemDescription) {
		SearchResultsItemCandidates item = (SearchResultsItemCandidates)itemDescription;
		
		if (item == null) {
			basePanel.setVisible(false);
		} else {
			basePanel.setVisible(true);
		}
		
		if (item.getCandName() == null) {
			candName.setText("");
			candName.setVisible(false);
		} else {
			candName.setText(item.getCandName());
			candName.setVisible(true);
		}
		
		if (item.getItemID() == null) {
			candID.setText("");
			candID.setVisible(false);
		} else {
			candID.setText(String.valueOf(item.getItemID()));
			candID.setVisible(true);
		}
		
		if (item.getRegionName() == null) {
			regionName.setText("");
			regionName.setVisible(false);
		} else {
			regionName.setText(item.getRegionName());
			regionName.setVisible(true);
		}
		
		if (item.getAgeView() == null) {
			age.setText("");
			age.setVisible(false);
		} else {
			age.setText(item.getAgeView());
			age.setVisible(true);
		}
		
		if (item.getCompensationView() == null) {
			compensation.setText("");
			compensation.setVisible(false);
		} else {
			compensation.setText(item.getCompensationView());
			compensation.setVisible(true);
		}
		
		String emplLine = composeEmplLine(item);
		if (emplLine != null && emplLine.length() > 0) {
			empl.setHTML(emplLine);
			empl.setVisible(true);
		} else {
			empl.setVisible(false);
			empl.setHTML("");
		}
		
		if (item.getCategories() == null) {
			categories.setText("");
			categories.setVisible(false);
		} else {
			categories.setText(item.getCategories());
			categories.setVisible(true);
		}
		
		if (item.getLanguages() == null) {
			languages.setText("");
			languages.setVisible(false);
		} else {
			languages.setText(item.getLanguages());
			languages.setVisible(true);
		}
		
		if (item.getContactsEmail() == null) {
			contactsEmail.setText("");
			contactsEmail.setVisible(false);
		} else {
			contactsEmail.setHTML("<a href=\"mailto:" + item.getContactsEmail() + "\">" + 
					item.getContactsEmail() + "</a>");
			contactsEmail.setVisible(true);
		}
		
		if (item.getContactsPhones() == null) {
			contactsPhones.setText("");
			contactsPhones.setVisible(false);
		} else {
			contactsPhones.setText(item.getContactsPhones());
			contactsPhones.setVisible(true);
		}
		
		String updatedLine = composeUpdatedLine(item);
		if (updatedLine != null && updatedLine.length() > 0) {
			updated.setHTML(updatedLine);
			updated.setVisible(true);
		} else {
			updated.setVisible(false);
			updated.setHTML("");
		}
		
		String orderLine = composeOrderLine(item);
		if (orderLine != null && orderLine.length() > 0) {
			order.setHTML(orderLine);
			order.setVisible(true);
		} else {
			order.setVisible(false);
			order.setHTML("");
		}
		
		if (item.getContactNote() == null) {
			contactNote.setText("");
			contactNote.setVisible(false);
		} else {
			contactNote.setText(item.getContactNote());
			contactNote.setVisible(true);
		}
		
		if (item.getContactCreatorName() == null) {
			contactCreatorName.setText("");
			contactCreatorName.setVisible(false);
		} else {
			contactCreatorName.setText(item.getContactCreatorName());
			contactCreatorName.setVisible(true);
		}
		
		if (item.getContactResult() == null) {
			contactResult.setText("");
			contactResult.setVisible(false);
		} else {
			contactResult.setText(item.getContactResult());
			contactResult.setVisible(true);
		}
		
		if (item.getContactType() == null) {
			contactType.setText("");
			contactType.setVisible(false);
		} else {
			contactType.setText(item.getContactType());
			contactType.setVisible(true);
		}
		
		if (item.getContactDate() == null) {
			contactDate.setText("");
			contactDate.setVisible(false);
		} else {
			contactDate.setText(DateTimeFormat.getShortDateFormat().format(item.getContactDate()));
			contactDate.setVisible(true);
		}
	}
	
	private String composeEmplLine(SearchResultsItemCandidates item) {
		StringBuffer sb = new StringBuffer();
		
		if (item.getEmplCompany() != null) {
			sb.append("<span class=\"RecordSummaryCandidatesView_emplComp\">" + 
					item.getEmplCompany() + 
					"</span>" + " ");	
		}
		
		if (item.getEmplTitle() != null) {
			sb.append("<span class=\"RecordSummaryCandidatesView_emplPos\">" + 
					item.getEmplTitle() + "</span>");	
		}
		
		return sb.toString();
	}

	private String composeOrderLine(SearchResultsItemCandidates item) {
		StringBuffer sb = new StringBuffer();
		
		if (item.getContactCompany() != null) {
			sb.append("<span class=\"RecordSummaryCandidatesView_actionComp\">" + 
					item.getContactCompany() + 
					"</span>" + " ");	
		}
		
		if (item.getContactPositionTitle() != null) {
			sb.append("<span class=\"RecordSummaryCandidatesView_actionPos\">" + 
					item.getContactPositionTitle() + 
					"</span>" + " ");
		}
		
		if (item.getContactComp() != null) {
			sb.append("<span class=\"RecordSummaryCandidatesView_actionMoney\">" + 
					item.getContactComp() + "</span>");	
		}
		
		return sb.toString();
	}
	
	private String composeUpdatedLine(SearchResultsItemCandidates item) {
		StringBuffer sb = new StringBuffer();
		
		sb.append("<span class=\"RecordSummaryCandidatesView_updated_l\">" + 
				"Updated:" + 
				"</span>" + " ");
		
		if (item.getUpdated() != null) {
			sb.append("<span class=\"RecordSummaryCandidatesView_updater\">" + 
					DateTimeFormat.getShortDateTimeFormat().format(item.getUpdated()) + 
					"</span>" + " ");	
		}
		
		if (item.getUpdaterName() != null) {
			sb.append("<span class=\"RecordSummaryCandidatesView_updated\">" + 
					item.getUpdaterName() + 
					"</span>" + " ");	
		}
		
		sb.append("<span class=\"RecordSummaryCandidatesView_created_l\">" + 
				"Created:" + 
				"</span>" + " ");
		
		if (item.getCreated() != null) {
			sb.append("<span class=\"RecordSummaryCandidatesView_created\">" + 
					DateTimeFormat.getShortDateTimeFormat().format(item.getCreated()) + 
					"</span>" + " ");	
		}
		
		if (item.getCreatorName() != null) {
			sb.append("<span class=\"RecordSummaryCandidatesView_creator\">" + 
					item.getCreatorName() + "</span>");	
		}
		
		return sb.toString();
	}
}
